#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
namespace sqy
{
    template<class T>
    class Less
    {
    public:
        bool operator()(const T& x,const T& y)
        {
            return x < y;
        }
    };

    template<class T>
    class Greater
    {
    public:
        bool operator()(const T& x,const T& y)
        {
            return x > y;
        }
    };
    template<class T, class Container = std::vector<T>, class Compare = Less<T>>
    class priority_queue
    {
    private:

        void AdjustUp(int child)
        {
            Compare con;
            int n = _con.size();
            int parent = (child - 1) / 2;
            while(child > 0)
            {
                if(con(_con[parent],_con[child]))
                {
                    swap(_con[parent],_con[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        void AdjustDown(int parent)
        {
            Compare con;
            int n = _con.size();
            int child = 2 * parent + 1;
            while(child < n)
            {
                if(child + 1 < n && con(_con[child],_con[child+1]))
                {
                    child++;
                }

                if(con(_con[parent], _con[child]))
                {
                    swap(_con[parent],_con[child]);
                    parent = child;
                    child = 2 * parent + 1;
                }
                else
                {
                    break;
                }
            }

        }

    public:
        priority_queue()
        {}
        template<class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while(first != last)
            {
                _con.push_back(*first);
                ++first;
            }

            for(int i = _con.size() - 2; i >= 0; i--)
            {
                AdjustDown(i);
            }
        }

        void pop()
        {
            assert(_con.size() != 0);
            swap(_con[0],_con[_con.size() - 1]);
            _con.pop_back();
            AdjustDown(0);
        }

        void push(const T& x)
        {
            _con.push_back(x);
            AdjustUp(_con.size() - 1);
        }

        const T& top()
        {
            assert(_con.size() != 0);
            return _con[0];
        }

        bool empty()
        {
            return _con.empty();
        }

        size_t size()
        {
            return _con.size();
        }
    private:
        Container _con;
    };

    void test_priority_queue()
    {
        // vector<int>v{2,1,6,4,78,34,8,43,87,7};
        priority_queue<int>pq;
        pq.push(3);
        pq.push(5);
        pq.push(1);
        pq.push(4);


        while(!pq.empty())
        {
            cout << pq.top() << endl;
            pq.pop();
        }
    }
}